import { NativeModules, Platform } from 'react-native'
import * as WeChat from "react-native-wechat"
import { captureRef } from 'react-native-view-shot';
import { Jx, Jpp } from "./Jx"
import { Dialog } from "./Dialog"
import { uQQ, cSys } from "./Sys"

export const SHARE_CAP = 1; // SHOT/网络图
export const SHARE_WCHAT = 1; 
export const SHARE_QQ = 1;
export const SHARE_SAVE = 1;
export const SHARE_ORI = 1; // 返回网络图
export const SHARE_FILE = 1; // file://
export const SHARE_64 = 1; // 返回base64
export const SHARE_MULTI = 1;
export const SHARE_URI = 1; // 返回 data:image/png;base64,

export class cShare{
    static promiseCapture(shotRef, result = "tmpfile"){
        // if(!shotRef){
        //     Dialog.toast("正在合成图片, 请稍后");
        //     return Promise.resolve(false);
        // }
        return new Promise(resolve=>{
            captureRef(shotRef, {
                format: "jpg",
                quality: 0.3,
                result,
                // result:"tmpfile",
                // result:"data-uri",
                // result:"base64",
            }).then(uri=>{
                resolve(uri);
            }).catch(err=>{
                Dialog.toast("无效图片");
                console.warn(Jx.stringify(err));
                resolve(false);
            });
        });
    }
    static promiseCapture64(shotRef){
        return this.promiseCapture(shotRef, "base64");
    }
    static promiseCaptureDataUri(shotRef){
        return this.promiseCapture(shotRef, "data-uri");
    }
    static toConfig(capture_wx_qq_save_multi){
        const cfg = capture_wx_qq_save_multi;
        if(cfg.SHARE_MULTI){
            // 现在 viewshot 安卓默认有file://
            return Jpp.isIOS ? { SHARE_URI } : { SHARE_FILE };
        }
        if(!cfg.SHARE_CAP && (Jpp.isIOS || cfg.SHARE_QQ)){
            // 安卓只有QQ可以保存网络图片
            return {SHARE_ORI};
        }
        if(Jpp.isAndroid && cfg.SHARE_QQ){
            return {SHARE_64};
        }
        if(cfg.SHARE_WCHAT && Jpp.isIOS){
            // ios微信好生奇怪 需要 file://
            return {SHARE_FILE};
        }
        // 安卓cap保存相册可以不加(已经是本地文件了)
        return {};
    }

    static shareQQ(image, Zone){
        // if(!this.isQQInstalled(true)){
        //     return Promise.resolve(false);
        // }
        return uQQ.share(image, Zone);
    }

    static shareWchat(imageUrl, moments){
        // if(!this.isWechatInstalled(true)){
        //     return Promise.resolve(false);
        // }
        imageUrl = {type: 'imageFile', imageUrl}
        // if(Jpp.isIOS && !(imageUrl.includes("http://") || imageUrl.includes("https://"))){ 
        //     imageUrl.type = 'imageUrl';
        // }
        moments = moments ? WeChat.shareToTimeline(imageUrl) : WeChat.shareToSession(imageUrl);
        return new Promise(resolve=>{
            moments.then(r=>{
                // 安卓进不到这里 (尽早取消转圈圈)
                console.warn(Jx.stringify(r));
                if(r.errCode == 0){
                    // ios 取消也进这里
                    // Dialog.toast("分享成功");
                    resolve(true);
                }else if(r.errStr){
                    Dialog.toast(r.errStr);
                    resolve(false);
                }else{
                    Dialog.toast("分享失效");
                    resolve(false);
                }
            }).catch(err=>{
                Dialog.toast("分享失败");
                if (err instanceof WeChat.WechatError) {
                    console.warn(e.stack);
                }
                resolve(false);
            })
        });
    }
}


export class cShareInfo {
    // 一变全变 静态全局变量是不是坑了你很久
    static empty(){
        return {
            imageShot : null,
            imageFile : null,
            image64 : null,
            imageUri : null
        }
    }
    init(props){
        const { isCapture, imageOri, shotRef, shot, onReady, onShot } = props;
        if(!imageOri && !shotRef){
            console.warn("cShareInfo 未加载图片/Shot");
            Dialog.toast("分享图片加载失败");
        }
        // this.imageQQ = null;
        // this.imageWchat = null;
        // this.imageSave = null;

        this.isCapture = isCapture;
        this.imageOri = imageOri;
        this.shotRef = shotRef;
        this.shot = shot || cShareInfo.empty();
        this.onReady = onReady || (()=>true);
        this.onShot = onShot || (shot=>null);
    }

    promiseConfig(cfg){
        if(cfg.SHARE_ORI){
            // console.warn("SHARE_ORI");
            return Promise.resolve(this.imageOri)
        }
        if(cfg.SHARE_64){
            if(this.shot.image64){
                // console.warn("SHARE_64");
                return Promise.resolve(this.shot.image64);
            }
        } else if(cfg.SHARE_FILE){
            if(this.shot.imageFile){
                // console.warn("SHARE_FILE");
                return Promise.resolve(this.shot.imageFile);
            }
        } else if(cfg.SHARE_URI){
            if(this.shot.imageUri){
                // console.warn("SHARE_URI");
                return Promise.resolve(this.shot.imageUri);
            }
        } else if(this.shot.imageShot){
            // console.warn("imageShot");
            return Promise.resolve(this.shot.imageShot);
        }
        // console.warn("promiseCaptureWithoutOri");
        return new Promise(resolve=>{
            const pro = (cfg.SHARE_64 || cfg.SHARE_URI) ? 
                cShare.promiseCapture64(this.shotRef) : 
                cShare.promiseCapture(this.shotRef);
            pro.then(image=>{
                let i = image;
                if(image){
                    if(cfg.SHARE_64 || cfg.SHARE_URI){
                        this.shot.image64 = image;
                        if(cfg.SHARE_URI){
                            i = "data:image/jpeg;base64," + image;
                            this.shot.imageUri = i;
                        }
                    }else{
                        this.shot.imageShot = image;
                        if(cfg.SHARE_FILE){
                            i = cSys.toImageFile(image);
                            this.shot.imageFile = i;
                        }
                    }
                    this.onShot(this.shot);
                }
                resolve(i);
            }).catch(err=>{
                resolve(false);
            })
        })
    }

    promiseImage(cfg){
        if(!this.onReady()){
            return Promise.resolve(false);
        }
        if(cfg.SHARE_WCHAT && !cSys.isWechatInstalled(true)){
            return Promise.resolve(false);
        }
        if(cfg.SHARE_QQ && !cSys.isQQInstalled(true)){
            return Promise.resolve(false);
        }
        if(this.isCapture){
            cfg.SHARE_CAP = 1;
        }
        return this.promiseConfig(cShare.toConfig(cfg));
    }
}